package com.fly.sms;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.fly.sms.progress.ThreadProcess;
import com.fly.sms.service.CustomerService;
import com.fly.sms.util.C;
import com.fly.sms.util.StringUtil;

public class ChgpassActivity extends BaseActivity implements
		View.OnClickListener {
	EditText oldPass;
	EditText newPass;
	EditText newPass2;
	Button submitBtn;
	Button cancleBtn;

	boolean flag;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View view = this.getView(R.layout.chgpass);
		content.addView(view);
		title.setText(R.string.nav2_customer_chgpass);
		oldPass = (EditText) findViewById(R.id.oldPass);
		newPass = (EditText) findViewById(R.id.newPass);
		newPass2 = (EditText) findViewById(R.id.newPass2);
		submitBtn = (Button) findViewById(R.id.submitBtn);
		cancleBtn = (Button) findViewById(R.id.cancleBtn);
		submitBtn.setOnClickListener(this);
		cancleBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.submitBtn:
			if (StringUtil.isEmpty(oldPass.getText().toString())) {
				this.showMsg(getText(R.string.oldpass));
				oldPass.setFocusable(true);
				oldPass.setFocusableInTouchMode(true);
				oldPass.requestFocus();
				return;
			}
			if(!StringUtil.getMD5(oldPass.getText().toString()).equals(customer.get("LoginPass").toString())){
				this.showMsg(getText(R.string.oldpasserror));
				oldPass.setFocusable(true);
				oldPass.setFocusableInTouchMode(true);
				oldPass.requestFocus();
				return;
			}
			if (StringUtil.isEmpty(newPass.getText().toString())) {
				this.showMsg(getText(R.string.newpass));
				newPass.setFocusable(true);
				newPass.setFocusableInTouchMode(true);
				newPass.requestFocus();
				return;
			}
			if (StringUtil.isEmpty(newPass2.getText().toString())) {
				this.showMsg(getText(R.string.newpass2));
				newPass2.setFocusable(true);
				newPass2.setFocusableInTouchMode(true);
				newPass2.requestFocus();
				return;
			}
			if (!newPass2.getText().toString().equals(newPass.getText().toString())) {
				this.showMsg(getText(R.string.newpassunsame));
				newPass2.setFocusable(true);
				newPass2.setFocusableInTouchMode(true);
				newPass2.requestFocus();
				return;
			}
			new ThreadProcess(this,new ThreadProcess.onProcess() {
				
				@Override
				public void onStart() {
					flag=CustomerService.chgpass(customer.get("LoginName").toString(), StringUtil.getMD5(oldPass.getText().toString()), StringUtil.getMD5(newPass.getText().toString()));
				}
				
				@Override
				public void onFinished() {
					if(flag){
						showMsg(getText(R.string.oprsuccess));
						customer.put("LoginPass", StringUtil.getMD5(newPass.getText().toString()));
						finish();
					}else{
						showMsg(getText(R.string.oprfail));
					}
				}
			}).startProgress();
			break;
		case R.id.cancleBtn:
			finish();
			break;
		}
	}
}